function ChansEnhanced_wakaba_IsError(doc)
{
	if (doc.location.href.indexOf('/wakaba.pl/') != -1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
function ChansEnhanced_wakaba_ProcessError(doc,page)
{
	ChansEnhanced_Log("ChansEnhanced_wakaba_ProcessError()");
	var hr = ChansEnhanced_evaluateXPath(doc, "/html/body/hr", page.RE);
	
	var form = ChansEnhanced_BuildReplyForm(doc);
	if (hr[0] && hr[0].nextSibling.nextSibling.tagName.toLowerCase() == "hl")
	{
		ChansEnhanced_Log("\tAppending form to <hl>");
		hr[0].nextSibling.nextSibling.insertBefore(form,hr[0].nextSibling.nextSibling.childNodes[2]);
	}
	else
	{
		var body = ChansEnhanced_evaluateXPath(doc, "/html/body", page.RE);
		ChansEnhanced_Log("\tAppending form to <body>");
		body[0].appendChild(form);
	}
	ChansEnhanced_ProcessError(doc,page)
	return 1;
}
function ChansEnhanced_wakaba_Process(doc,page)
{
	ChansEnhanced_Log("ChansEnhanced_wakaba_Process()");
	
	var topform = ChansEnhanced_evaluateXPath(doc, "/html/body/div/form", page.RE);
	var delform = ChansEnhanced_evaluateXPath(doc, "/html/body/form",page.RE);
	if (topform.length == 1 && delform.length == 1)
	{
		ChansEnhanced_Log("\tSeems like wakaba, trying to process...");
		page['form']	= topform;
		page['texta']	= ChansEnhanced_evaluateXPath(doc, "/html/body/div/form//textarea", page.RE);
		page['inputs']	= ChansEnhanced_evaluateXPath(doc, "/html/body/div/form//input", page.RE);		
		page['capt']	= ChansEnhanced_evaluateXPath(doc, "/html/body/div/form//img", page.RE);
		
		/*
		page['threads'] = ChansEnhanced_evaluateXPath(doc, "/html/body/form/div", page.RE);
		
		*/
		
		if (page.capt.length)
			ChansEnhanced.Pages[page.PageID].Captcha = page.capt[0].src;
		ChansEnhanced_ProcessPostForm(page.form[0],page.inputs,page.texta[0],page.PageID);	
		ChansEnhanced_Log("\tAdding EventListeners");
		
		var posts = ChansEnhanced_evaluateXPath(doc, "/html/body/form//*[@class='reflink']/a", page.RE);
		if (posts.length)
		for (var i=0; i < posts.length; i++)
		{
			var a = posts[i];
			if (doc.location.href.indexOf('/res/') > 0)
			{
				if (i)
					a.addEventListener("click",ChansEnhanced_wakaba_ShowReplyBoxPost,true);
				else
					a.addEventListener("click",ChansEnhanced_wakaba_ShowReplyBoxThread,true);
			}
			else
			{
				var href = a.getAttribute('href');
				var res  = href.match(/\/(\d+)\.x?h?t?ml?(#i?(\d+))?/);
				if (res.length && (res.length == 1 || (res[1] == res[3])))
				{
					a.addEventListener("click",ChansEnhanced_wakaba_ShowReplyBoxThread,true);
				}
				else
				{
					a.addEventListener("click",ChansEnhanced_wakaba_ShowReplyBoxPost,true);
				}
			}
			var p = a.parentNode;
			while(p.nextSibling && !(p.nextSibling.tagName && p.nextSibling.tagName.toLowerCase() == 'blockquote'))
			{
				p = p.nextSibling;
			}
			if (p.nextSibling)
				p = p.nextSibling;
			
			var l = a.parentNode;
			while(l.previousSibling && !(l.previousSibling.tagName && l.previousSibling.tagName.toLowerCase() == 'label'))
			{
				l = l.previousSibling;
			}
			if (p.previousSibling)
				l = l.previousSibling;
			
			var replyto = l.childNodes[2];
			var cpn     = l.childNodes[4];
			
			if (p.innerHTML.indexOf(ChansEnhanced.ArchiveMark) != -1)
			{
				var imgArch = doc.createElement("img");
				l.insertBefore(imgArch,replyto);
				imgArch.src = 'chrome://chansenhanced/content/icons/rar.png';
			}
			if (cpn.lastChild && cpn.lastChild.tagName && cpn.lastChild.tagName.toLowerCase() == 'a' && cpn.lastChild.href.indexOf('sage') > 0)
			{
				var imgSage = doc.createElement("img");
				l.insertBefore(imgSage,replyto);
				imgSage.src = 'chrome://chansenhanced/content/icons/sage.png';
			}			
		}
		ChansEnhanced_Log("\tRemoving useless space-wasting shit");
		var body = ChansEnhanced_evaluateXPath(doc, "/html/body", page.RE);
		if (body[0])
		{
			var fl1 = 0;
			for (i = 0; i < body[0].childNodes.length; i++)
			{
				var el = body[0].childNodes[i];
				if (!(el.tagName && el.tagName.toLowerCase() == 'form') && !(fl1 && el.tagName && el.tagName.toLowerCase() == 'table'))
				{
					try 
					{
						el.style.display = 'none';
					}
					catch (e){}
				}
				else
					fl1 = 1;
			}
		}
		ChansEnhanced_Log("\tDone !");
	}
	else
	{
		ChansEnhanced_Log("\tIs not wakaba ! PANIC PANIC PANIC PANIC");
	}
}
function ChansEnhanced_wakaba_GetCaptchaURL(page)
{
	if (page.Captcha)
	{
		var cpr = page.Captcha;
		if (page.ThreadID)
		{
			cpr = cpr.replace(/key=[^\&]+/,'key=res'+page.ThreadID);
			return cpr + '&CERandom='+ChansEnhanced_Time()+ChansEnhanced_GetUniqueNumber();
		}
		else
		{
			cpr = cpr.replace(/key=[^\&]+/,'key=mainpage');
			return cpr + '&CERandom='+ChansEnhanced_Time()+ChansEnhanced_GetUniqueNumber();
		}
	}
}
function ChansEnhanced_wakaba_GetThreadID(a)
{
	if (a.ownerDocument.location.href.indexOf('/res/') > 0)
	{
		var href = a.ownerDocument.location.href;
	}
	else
	{
		var href = a.getAttribute('href');
	}
	var res = href.match(/\/(\d+)\.x?h?t?ml?(#i?(\d+))?/);
	return [res[1],res[3]?res[3]:0];
}
function ChansEnhanced_wakaba_ShowReplyBoxThread(event)
{
	var form = this.ownerDocument.getElementById('CEForm');
	if (form == null)
	{
		form = ChansEnhanced_BuildReplyForm(this.ownerDocument);
	}
	var node = this.parentNode;
	while ((!node.tagName || node.tagName.toLowerCase() != "blockquote"))
	{
		node = node.nextSibling;
	}
	
	var PageID = ChansEnhanced_GetPageID(this.ownerDocument);
	var TID = ChansEnhanced_wakaba_GetThreadID(this);
	ChansEnhanced.Pages[PageID].ThreadID = TID[0];
	ChansEnhanced.Pages[PageID].PostID   = TID[1];
	
	node.parentNode.insertBefore(form, node.nextSibling);
	event.preventDefault();
}
function ChansEnhanced_wakaba_ShowReplyBoxPost(event)
{
	var table = this.parentNode;

	while (table.tagName.toLowerCase() != "table")
	{
		table = table.parentNode;
	}

	var form = this.ownerDocument.getElementById('CEForm');
	if (form == null)
	{
		form = ChansEnhanced_BuildReplyForm(this.ownerDocument);
	}
	
	var PageID = ChansEnhanced_GetPageID(this.ownerDocument);
	var TID = ChansEnhanced_wakaba_GetThreadID(this);
	ChansEnhanced.Pages[PageID].ThreadID = TID[0];
	ChansEnhanced.Pages[PageID].PostID   = TID[1];
	
	table.parentNode.insertBefore(form, table.nextSibling);
	
	event.preventDefault();
}